package com.ymatou.iapi.productmanager.testcase.old;

import com.ymatou.iapi.productmanager.parameter.SetRecommendBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.SetRecommendCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;

import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;
/**
*设置直播橱窗商品
*FreeMaker Template自动生成代码
*/
@TestSuite
@Ignore
public class Ts_SetRecommend{
	private static SetRecommendBean setrecommendBean;
	private static SetRecommendCall setrecommendCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置直播橱窗商品");
	}

	@Before
	public void caseUp() {
		setrecommendBean=new SetRecommendBean();
		setrecommendCall=new SetRecommendCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setrecommendCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetRecommend_001() {
		Logger.start(true,"设置直播橱窗商品,商品已是推荐商品");
		try {
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			ProductManagerCallServiceV2.addProductInLives(liveId, Arrays.asList(productId), sellerId);
			new YmtProductsIWapper().updateProductsInLiveTop(productId, sellerId, 1);
			
			setrecommendBean.setActivityId(liveId);
			setrecommendBean.setProductId(productId);
			setrecommendBean.setUserId(sellerId);
			setrecommendCall.setData(setrecommendBean);
			setrecommendCall.callService();			
			Logger.verifyEquals("201", setrecommendCall.getString("Code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SetRecommend_002() {
		Logger.start(true,"设置直播橱窗商品,推荐商品数目大于10,设置失败");
		try {
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			for(int i=0;i<10;i++){
				String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
				int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
				ProductManagerCallServiceV2.addProductInLives(liveId, Arrays.asList(productId), sellerId);

				setrecommendBean.setActivityId(liveId);
				setrecommendBean.setProductId(productId);
				setrecommendBean.setUserId(sellerId);
				setrecommendCall.setData(setrecommendBean);
				setrecommendCall.callService();
				new YmtProductsIWapper().updateProductsInLiveTop(productId, sellerId, 1);
				Logger.verifyEquals("200", setrecommendCall.getString("Code"), "验证Code");
			}
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			ProductManagerCallServiceV2.addProductInLives(liveId, Arrays.asList(productId), sellerId);

			setrecommendBean.setActivityId(liveId);
			setrecommendBean.setProductId(productId);
			setrecommendBean.setUserId(sellerId);
			setrecommendCall.setData(setrecommendBean);
			setrecommendCall.callService();
			new YmtProductsIWapper().updateProductsInLiveTop(productId, sellerId, 1);
			Logger.verifyEquals("201",setrecommendCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetRecommend_003() {
		Logger.start(true,"设置直播橱窗商品");
		try {
			AddSellerProductRequest addProdReq =ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			ProductManagerCallServiceV2.addProductInLives(liveId, Arrays.asList(productId), sellerId);

			setrecommendBean.setActivityId(liveId);
			setrecommendBean.setProductId(productId);
			setrecommendBean.setUserId(sellerId);
			setrecommendCall.setData(setrecommendBean);
			setrecommendCall.callService();
			Logger.verifyEquals("200", setrecommendCall.getString("Code"), "验证Code");
			Integer itop = new YmtProductsIWapper().getProductsInLiveTop(productId,sellerId);
			Logger.verifyEquals(1, itop, "验证iIstop");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetRecommend_004() {
		Logger.start(true,"设置直播橱窗商品,商品不存在");
		try {
			String productId = "p4066379";
			int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			ProductManagerCallServiceV2.addProductInLives(liveId, Arrays.asList(productId), sellerId);
			new YmtProductsIWapper().updateProductsInLiveTop(productId, sellerId, 1);

			setrecommendBean.setActivityId(liveId);
			setrecommendBean.setProductId(productId);
			setrecommendBean.setUserId(sellerId);
			setrecommendCall.setData(setrecommendBean);
			setrecommendCall.callService();
			Logger.verifyEquals("201", setrecommendCall.getString("Code"), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}